package com.itheima.controller;

import com.itheima.pojo.PmsProductAttribute;
import com.itheima.service.PmsProductAttributeService;
import com.itheima.vo.PageResultVo;
import com.itheima.vo.ProductAttrInfo;
import com.itheima.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("productAttribute")
public class PmsProductAttributeController {
    @Autowired
    private PmsProductAttributeService pmsProductAttributeService;

    /**
     * 根据分类查询属性列表或参数列表
     * @param productCategoryId 商品分类id
     * @return
     */
    @GetMapping("attrInfo/{productCategoryId}")
    public Result getAttrInfo(@PathVariable("productCategoryId") Integer productCategoryId){
        List<ProductAttrInfo> list = pmsProductAttributeService.getAttrInfo(productCategoryId);
        return Result.success(list);
    }

    /**
     * 添加商品属性信息
     * @param productAttributeParam
     * @return
     */
    @PostMapping("create")
    public Result create(@RequestBody PmsProductAttribute productAttributeParam){
        pmsProductAttributeService.create(productAttributeParam);
        return Result.success(null);
    }

    /**
     * 批量删除商品属性
     * @param ids
     * @return
     */
    @PostMapping("delete")
    public Result deleteByIds(@RequestParam List<Integer> ids){
        pmsProductAttributeService.deleteByIds(ids);
        return Result.success(null);
    }

    /**
     * 根据分类查询属性列表或参数列表
     * @param cid
     * @param type
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("list/{cid}")
    public Result list(@PathVariable("cid") Integer cid,Integer type,@RequestParam(defaultValue = "1")Integer pageNum,
                       @RequestParam(defaultValue = "10") Integer pageSize){
        PageResultVo<PmsProductAttribute> resultVo=pmsProductAttributeService.list(cid,type,pageNum,pageSize);
        return Result.success(resultVo);
    }

    /**
     * 修改商品属性信息
     * @param id
     * @param productAttributeParam
     * @return
     */
    @PostMapping("update/{id}")
    public Result updateById(@PathVariable("id")Integer id,@RequestBody PmsProductAttribute productAttributeParam){
        pmsProductAttributeService.updateById(id,productAttributeParam);
        return Result.success(null);
    }

    /**
     * 查询单个商品属性
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Result getProductAttributeById(@PathVariable("id")Integer id){
        PmsProductAttribute pmsProductAttribute=pmsProductAttributeService.getProductAttributeById(id);
        return Result.success(pmsProductAttribute);
    }
}
